/*
	Copyright 2009 Benjamin Vedder	vedder87@gmail.com
	
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    */

package com.google.code.retrobox.editor.tiles;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.swing.*;

import com.google.code.retrobox.editor.dataCollections.TileData;
import com.google.code.retrobox.editor.dataCollections.TileSet;


public class TileSetPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// Stuff
	//private TileSet tileSet;
	
	private Tile currentTile;
	
	private int		WIDTH = 16, 
					HEIGHT = 16, 
					VERT_TILES = 8;
	
	private List<ActionListener> listeners = new LinkedList<ActionListener>();
	private List<Tile> tileList = new ArrayList<Tile>();
	
	// Panels
	private JPanel currentPanel = new JPanel(new BorderLayout());
	
	// Components
	private JLabel idLabel = new JLabel();
		
	public TileSetPanel(TileSet tileSet) {
		setLayout(new BorderLayout());
		
		//this.tileSet = tileSet;
		
		JPanel pal = new JPanel(new GridLayout(VERT_TILES, tileSet.getLength() 
				/ VERT_TILES));
		
		for (char i = 0;i < tileSet.getLength();i++) {
			Tile t = new Tile(null, tileSet.getTile(i));
			t.setPixelsPerPixel(WIDTH, HEIGHT);
			t.setHighlight(true);
			t.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					Tile t = (Tile)arg0.getSource();
					currentTile = t;
					update();
				}
				
			});
			t.setToolTipText("Tile number: " + (int)i);
			pal.add(t);
			tileList.add(t);
		}
		
		currentPanel.add(idLabel, BorderLayout.NORTH);
		
		currentTile = tileList.get(0);
		
		update();
		
		add(pal, BorderLayout.CENTER);
		add(currentPanel, BorderLayout.WEST);
		
		setPreferredSize(new Dimension((tileSet.getLength() / VERT_TILES) * WIDTH, VERT_TILES * HEIGHT));
	}
	
	public TileData getCurrentTile() {
		return currentTile.getTileData();
	}
	
	public void setCurrentTile(TileData tile) {
		for (Tile element: tileList) {
			if (element.getTileData() == tile) {
				currentTile = element;
			}
		}
		update();
	}
	
	public void update() {
		try {
			currentPanel.remove(currentTile);
		} catch(Exception e) {
			
		}
		currentTile = new Tile(null, currentTile.getTileData());
		currentTile.setPixelsPerPixel(48, 48);
		currentPanel.add(currentTile, BorderLayout.SOUTH);

		idLabel.setText("<html>" + currentTile.getTileData().index + "<BR>" + 
				String.format("0x%X", currentTile.getTileData().index) + "</html>");
		
		validate();
		
		for (ActionListener element: listeners) {
			element.actionPerformed(new ActionEvent(this, 0, "New tile selected"));
		}
	}
	
	public void addActionListener(ActionListener w) {
		listeners.add(w);
	}
}
